/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.costbreakdowm.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import com.thinkgem.jeesite.common.beanvalidator.BeanValidators;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.utils.excel.ExportExcel;
import com.thinkgem.jeesite.common.utils.excel.ImportExcel;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.costbreakdowm.entity.EmployerCostBreakdown;
import com.thinkgem.jeesite.modules.costbreakdowm.service.EmployerCostBreakdownService;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.SystemService;

/**
 * 雇主其它成本明细Controller
 * @author zxl
 * @version 2017-08-12
 */
@Controller
@RequestMapping(value = "${adminPath}/costbreakdowm/employerCostBreakdown")
public class EmployerCostBreakdownController extends BaseController {

	@Autowired
	private EmployerCostBreakdownService employerCostBreakdownService;
	
	@ModelAttribute
	public EmployerCostBreakdown get(@RequestParam(required=false) String id) {
		EmployerCostBreakdown entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = employerCostBreakdownService.get(id);
		}
		if (entity == null){
			entity = new EmployerCostBreakdown();
		}
		return entity;
	}
	
	@RequiresPermissions("costbreakdowm:employerCostBreakdown:view")
	@RequestMapping(value = {"list", ""})
	public String list(EmployerCostBreakdown employerCostBreakdown, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<EmployerCostBreakdown> page = employerCostBreakdownService.findPage(new Page<EmployerCostBreakdown>(request, response), employerCostBreakdown); 
		model.addAttribute("page", page);
		return "modules/costbreakdowm/employerCostBreakdownList";
	}

	@RequiresPermissions("costbreakdowm:employerCostBreakdown:view")
	@RequestMapping(value = "form")
	public String form(EmployerCostBreakdown employerCostBreakdown, Model model) {
		model.addAttribute("employerCostBreakdown", employerCostBreakdown);
		return "modules/costbreakdowm/employerCostBreakdownForm";
	}

	@RequiresPermissions("costbreakdowm:employerCostBreakdown:edit")
	@RequestMapping(value = "save")
	public String save(EmployerCostBreakdown employerCostBreakdown, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, employerCostBreakdown)){
			return form(employerCostBreakdown, model);
		}
		employerCostBreakdownService.save(employerCostBreakdown);
		addMessage(redirectAttributes, "保存雇主其它成本明细成功");
		return "redirect:"+Global.getAdminPath()+"/costbreakdowm/employerCostBreakdown/?repage";
	}
	
	@RequiresPermissions("costbreakdowm:employerCostBreakdown:edit")
	@RequestMapping(value = "delete")
	public String delete(EmployerCostBreakdown employerCostBreakdown, RedirectAttributes redirectAttributes) {
		employerCostBreakdownService.delete(employerCostBreakdown);
		addMessage(redirectAttributes, "删除雇主其它成本明细成功");
		return "redirect:"+Global.getAdminPath()+"/costbreakdowm/employerCostBreakdown/?repage";
	}
	/**
	 * 导出用户数据
	 * @param user
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("costbreakdowm:employerCostBreakdown:view")
	@RequestMapping(value = "export", method= RequestMethod.POST)
	public String exportFile(EmployerCostBreakdown user, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
			String fileName = "雇主其它成本数据"+ DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
			Page<EmployerCostBreakdown> page = employerCostBreakdownService.findPage(new Page<EmployerCostBreakdown>(request, response, -1), user);
			new ExportExcel("雇主其它成本数据", EmployerCostBreakdown.class).setDataList(page.getList()).write(response, fileName).dispose();
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出雇主其它成本数据失败！失败信息："+e.getMessage());
		}
		return "redirect:" + adminPath + "/costbreakdowm/employerCostBreakdown/list?repage";
	}

	/**
	 * 导入用户数据
	 * @param file
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("costbreakdowm:employerCostBreakdown:view")
	@RequestMapping(value = "import", method=RequestMethod.POST)
	public String importFile(MultipartFile file, RedirectAttributes redirectAttributes) {
		try {
			int successNum = 0;
			int failureNum = 0;
			StringBuilder failureMsg = new StringBuilder();
			ImportExcel ei = new ImportExcel(file, 0, 0);
			List<EmployerCostBreakdown> list = ei.getDataList(EmployerCostBreakdown.class);
			for (EmployerCostBreakdown user : list){

			}
			if (failureNum>0){
				failureMsg.insert(0, "，失败 "+failureNum+" 条用户，导入信息如下：");
			}
			addMessage(redirectAttributes, "已成功导入 "+successNum+" 条用户"+failureMsg);
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入用户失败！失败信息："+e.getMessage());
		}
		return "redirect:" + adminPath + "/costbreakdowm/employerCostBreakdown/list?repage";
	}
}